package com.ansion.utils;

import android.content.res.AssetFileDescriptor;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.io.Closeable;
import java.net.DatagramSocket;
import java.net.Socket;

/**
 * @author john on 2021/6/14.
 * @version 1.0
 * @desc
 */
public class CloseUtils {
    public static void silentlyClose(Closeable c) {
        if (c != null) {
            try {
                c.close();
            } catch (Throwable e) {
            }
        }
    }

    //todo lv3 rename method
    public static void silentlyClose(Cursor c) {
        if (c != null) {
            try {
                c.close();
            } catch (Throwable e) {
            }
        }
    }

    public static void silentlyClose(SQLiteDatabase c) {
        if (c != null) {
            try {
                c.close();
            } catch (Throwable e) {
            }
        }
    }

    public static void silentlyClose(Socket c) {
        if (c != null) {
            try {
                c.close();
            } catch (Throwable e) {
            }
        }
    }

    public static void silentlyClose(DatagramSocket c) {
        if (c != null) {
            try {
                c.close();
            } catch (Throwable e) {
            }
        }
    }

    public static void silentlyClose(AssetFileDescriptor afd) {
        if (afd != null) {
            try {
                afd.close();
            } catch (Throwable w) {
            }
        }
    }

    public static void closeQuietly(Cursor cursor) {
        silentlyClose(cursor);
    }
}
